package com.jc.dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

//使用泛型
public class BaseDao<T> {
	@Resource
	private SessionFactory sessionFactory;
	private  Class<T> clazz;
	public BaseDao(){
		//通过反射机制获取泛型类型
		Type type =this.getClass().getGenericSuperclass();
		ParameterizedType ptype=(ParameterizedType)type;
		clazz = (Class<T>) ptype.getActualTypeArguments()[0];
	}
	public boolean add(T obj){
		
		return false;
	}
	
	public boolean delete(T obj){
		return false;
	}
	
	public List<T> findAll(){
		String hql="from "+clazz.getSimpleName();
		System.out.println(hql);
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		List<T> list = (List<T>)query.list();
		return list;
	}
}
